//
//  XDLMSanPostListViewController.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/2/17.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//*********散标列表

#import "XDLMSanPostListViewController.h"
#import "XDLMSanPostListTableViewCell.h"
#import "XDLMSanPostListModel.h"
#import "AFHTTPSessionManager+POST.h"
#import "SVPullToRefresh.h"

#import "ZHRTRequestFailureShowViewOrDetail.h"
#define TCOLOR [UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:115.0/255.0 alpha:1]
@interface XDLMSanPostListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, strong)XDLMSanPostListModel *model;
@end

@implementation XDLMSanPostListViewController
{
    UITableView *_table;
    UIButton *_rateBtn;//最新
    UIButton *_limitBtn;//利率按钮
    BOOL _isRateSelect;
    BOOL _isLimitSelect;
    NSString *_orderFlag;
    NSString *_page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = @"0";
    _orderFlag = @"1";
    _isRateSelect = YES;
    _isLimitSelect = YES;
    [self addViews];
    [self loadData];
    
    // Do any additional setup after loading the view.
}
- (void)loadData
{
    NSDictionary *param = @{@"method":@"sanPostProduct",@"page":_page,@"orderFlag":_orderFlag};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject)
    {
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            NSLog(@"%@",responseObject);
            [self.dataSource removeAllObjects];
            NSArray *array = responseObject[@"result"][@"indexPosts"];
            for (NSDictionary *dic in array) {
                self.model = [[XDLMSanPostListModel alloc] initWithDictionary:dic error:nil];
                [self.dataSource addObject:self.model];
            }
            if (self.dataSource.count == 0) {
                [ZHRTRequestFailureShowViewOrDetail showNoDataForView:self.view imageNamed:@"noPublic" text:@"暂无公告" type:0];
            }
            [_table reloadData];
        
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error)
    {
        
    }];
}
- (void)addViews
{
        UIView *topView = [self setTopView];
        [self.view addSubview:topView];
        
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 40*kscale, kScreenW, kScreenH - 64-40*kscale)];
        _table.delegate = self;
        _table.dataSource = self;
        //    _table.bounces = NO;
        _table.showsVerticalScrollIndicator = NO;
        [_table registerClass:[XDLMSanPostListTableViewCell class] forCellReuseIdentifier:@"XDLMSanPostListTableViewCell"];
        [self.view addSubview:_table];
}
#pragma mark -- UITableViewDataSource && Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDLMSanPostListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDLMSanPostListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSource[indexPath.row];
        return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90*kscale;
}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    return [self setTopView];
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 40*kscale;
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UIView *)setTopView
{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 40*kscale)];
    backView.backgroundColor = [UIColor whiteColor];
    UILabel *lineLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 39*kscale, kScreenW, 0.5)];
    lineLab.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1];
    [backView addSubview:lineLab];
    UIButton *mostNewBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenW/3, 40*kscale)];
    [mostNewBtn setTitle:@"最新" forState:UIControlStateNormal];
    [mostNewBtn setTitleColor:TCOLOR forState:UIControlStateNormal];
    mostNewBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [mostNewBtn addTarget:self action:@selector(mostNewBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:mostNewBtn];
    
    _rateBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW/3+(kScreenW/3-40*kscale)/2, (40-15)/2, 40*kscale, 15*kscale)];
    [_rateBtn setBackgroundImage:[UIImage imageNamed:@"rateup"] forState:UIControlStateNormal];
    [_rateBtn addTarget:self action:@selector(rateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [backView addSubview:_rateBtn];
    
    _limitBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW/3*2+(kScreenW/3-40*kscale)/2, (40-15)/2, 40*kscale, 15*kscale)];
    [_limitBtn setBackgroundImage:[UIImage imageNamed:@"limitup"] forState:UIControlStateNormal];
    [_limitBtn addTarget:self action:@selector(limitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:_limitBtn];
//    UILabel *lineTwoLab  = [[UILabel alloc] initWithFrame:CGRectMake(0, 40*kscale, kScreenW, 0.5)];
//    lineTwoLab.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1];
//    [backView addSubview:lineTwoLab];
        return backView;
}
#pragma mark --点击方法
- (void)rateBtnClick
{
    if (_isRateSelect == YES) {
        [_rateBtn setBackgroundImage:[UIImage imageNamed:@"ratedown"] forState:UIControlStateNormal];
        _isRateSelect = NO;
        _orderFlag = @"2";
    }
    else
    {
        [_rateBtn setBackgroundImage:[UIImage imageNamed:@"rateup"] forState:UIControlStateNormal];
        _isRateSelect = YES;
        _orderFlag = @"3";
    }

    [self loadData];
    
}
- (void)limitBtnClick
{
    if (_isLimitSelect == YES) {
        [_limitBtn setBackgroundImage:[UIImage imageNamed:@"limitdown"] forState:UIControlStateNormal];
        _isLimitSelect = NO;
        _orderFlag = @"4";
    }
    else
    {
        [_limitBtn setBackgroundImage:[UIImage imageNamed:@"limitup"] forState:UIControlStateNormal];
        _isLimitSelect = YES;
        _orderFlag = @"5";
    }

    [self loadData];
}
- (void)mostNewBtnClick
{
    _orderFlag = @"1";
    [self loadData];
}
#pragma mark --下拉刷新
- (void)pushtoreload
{
    __weak typeof(self) weakSelf = self;
    //下拉刷新
    [_table addPullToRefreshWithActionHandler:^{
        [weakSelf loadData];
    }];
    [_table.pullToRefreshView setTitle:@"松开立即刷新" forState:SVPullToRefreshStateTriggered];
    [_table.pullToRefreshView setTitle:@"数据正在努力加载中......" forState:SVPullToRefreshStateLoading];
    [_table.pullToRefreshView setTitle:@"刷新完成" forState:SVPullToRefreshStateStopped];
    [_table addInfiniteScrollingWithActionHandler:^{
        
//        [weakSelf loadmoreData];
        
    }];
}

#pragma mark -- Getter
- (NSMutableArray *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"散标理财";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
